package tunny

import (
	"fmt"
	"zinxtcp/route"
	"zinxtcp/ziface"
)

var LabourPool *Pool

func init() {
	LabourPool = New(100, NewLabour)
	fmt.Println("LabourPool inited.")
}

type Labour struct {
	readyChan chan bool
}

func NewLabour() Worker {
	labour := &Labour{
		readyChan: make(chan bool, 1),
	}
	labour.readyChan <- true
	return labour
}

func (l *Labour) Process(payload interface{}) (feedback interface{}) {
	defer func() {
		l.readyChan <- true
	}()
	sockPack, ok := payload.(ziface.ISockPack)
	if !ok {
		fmt.Println(fmt.Sprint("Labour Process error: payload is not type of ISockPack!"))
	}
	handler := route.Route.GetRouter(sockPack.GetMsgType())
	if handler == nil {
		fmt.Println(fmt.Sprint("api msg type = ", sockPack.GetMsgType(), " is not FOUND!"))
		return
	}
	handler.Handle(sockPack)
	return
}

func (l *Labour) BlockUntilReady() {
	<-l.readyChan
	return
}

func (l *Labour) Interrupt() {

}

func (l *Labour) Terminate() {

}
